home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr11 / pdox693.zip / TI1123.ASC < prev    next >
Text File  |  1992-10-19  |  7KB  |  265 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Paradox                               NUMBER  :  1123
  9.   VERSION  :  4.0
  10.        OS  :  DOS
  11.      DATE  :  October 19, 1992                         PAGE  :  1/4
  12.  
  13.     TITLE  :  An Introduction to DYNARRAY Branching
  14.  
  15.  
  16.  
  17.  
  18.   Prerequisites:
  19.   This Tech Info article assumes a basic working knowledge of the
  20.   following commands: PROC, EXECPROC, DYNARRAY, SWITCH and
  21.   SHOWPOPUP.  An intermediate to advanced level of PAL knowledge is
  22.   also recommended for full comprehension of the concepts involved.
  23.  
  24.  
  25.   The Concept:
  26.  
  27.   The concept of DYNARRAY Branching has its origin in the more
  28.   direct PAL command: SWITCH.  The standard SWITCH/CASE statement
  29.   is a method for evaluating a number of different logical
  30.   conditions (CASES) and performing the actions associated with the
  31.   first CASE in which the condition is true.
  32.  
  33.   Here is a simple example of a SWITCH statement which runs a
  34.   different procedure depending on which menu selection is chosen.
  35.   This example assumes that the three procedures C1, C2, and C3
  36.   have already been defined and reside in memory or a library in an
  37.   Autolib path.
  38.  
  39.   SHOWPOPUP "MenuList" CENTERED
  40.     "Choice1" : "First choice"  : "C1",
  41.     "Choice2" : "Second choice" : "C2",
  42.     "Choice3" : "Third choice"  : "C3"
  43.   ENDMENU
  44.   TO MenuSelection
  45.  
  46.   SWITCH
  47.     CASE MenuSelection = "C1" : C1()
  48.     CASE MenuSelection = "C2" : C2()
  49.     CASE MenuSelection = "C3" : C3()
  50.   ENDSWITCH
  51.  
  52.   With only three menu choices, the code above is succinct and
  53.   quick.  But what happens when you have thirty or forty menu
  54.   choices.  Not only must you define the thirty or forty procedures
  55.   but must also add new CASES to the SWITCH statement.  This
  56.   technique does not lend itself to the writing of reusable code or
  57.   easy modification.
  58.  
  59.   The use of the EXECPROC command can eliminate the necessity for a
  60.   lengthy SWITCH statement.  Before exploring this replacement
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Paradox                               NUMBER  :  1123
  75.   VERSION  :  4.0
  76.        OS  :  DOS
  77.      DATE  :  October 19, 1992                         PAGE  :  2/4
  78.  
  79.     TITLE  :  An Introduction to DYNARRAY Branching
  80.  
  81.  
  82.  
  83.  
  84.   technique here is a brief review of EXECPROC.  This command
  85.   executes a procedure by translating a string value into a
  86.   procedure call.  The procedure must have an empty parameter list.
  87.  
  88.   For example the following statement
  89.  
  90.     C1()
  91.  
  92.   would call the procedure C1.  This could also be accomplished
  93.   with the EXECPROC statement:
  94.  
  95.     EXECPROC "C1"
  96.  
  97.   A further replacement process would allow the procedure name
  98.   string to be derived from a variable.  For instance:
  99.  
  100.     ProcName = "C1"
  101.     EXECPROC ProcName
  102.  
  103.   This leads to the following code which replaces the SWITCH
  104.   statement in Example 1:
  105.  
  106.   SHOWPOPUP "MenuList" CENTERED
  107.     "Choice1" : "First choice on the menu"  : "C1",
  108.     "Choice2" : "Second choice on the menu" : "C2",
  109.     "Choice3" : "Third choice on the menu"  : "C3"
  110.   ENDMENU
  111.   TO MenuSelection
  112.  
  113.   EXECPROC MenuSelection
  114.  
  115.   One line of code can take the place of any number of separate
  116.   CASES.
  117.  
  118.   Note: The interpreter which enables EXECPROC to convert strings
  119.   to procedure calls may take a little time to load.  In some cases
  120.   EXECPROC may be slower than a short SWITCH statement.  The point
  121.   at which performance improves by using EXECPROC over SWITCH may
  122.   vary from machine to machine.  You will want to do some of your
  123.   own benchmarking if performance is a priority over generic
  124.   coding.  The performance benefits of EXECPROC over SWITCH seems
  125.   to start around 10 possible conditions.
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Paradox                               NUMBER  :  1123
  141.   VERSION  :  4.0
  142.        OS  :  DOS
  143.      DATE  :  October 19, 1992                         PAGE  :  3/4
  144.  
  145.     TITLE  :  An Introduction to DYNARRAY Branching
  146.  
  147.  
  148.  
  149.  
  150.   Up to this point Dynamic Arrays have not been an issue.  Similar
  151.   code could have been written using Paradox 3.5.  The necessity
  152.   for Dynamic Arrays comes in when you do not have a direct
  153.   correspondence between the string value returned by the menu and
  154.   the name of the procedure to be executed.
  155.  
  156.   For instance, if the tag of Choice1 in the SHOWPOPUP statement
  157.   was not also the name of the procedure to be called, the EXECPROC
  158.   statement alone would not call the correct procedure.  DYNARRAYs
  159.   allow a "map" to drawn between the possible returned values and
  160.   their corresponding procedures.
  161.  
  162.   Here is yet another version of Example 1.  This one demonstrates
  163.   this technique:
  164.  
  165.   DYNARRAY ProcNames[]
  166.   ProcNames["C1"] = "Option1"
  167.   ProcNames["C2"] = "Option2"
  168.   ProcNames["C3"] = "Option3"
  169.  
  170.   SHOWPOPUP "MenuList" CENTERED
  171.     "Choice1" : "First choice on the menu"  : "C1",
  172.     "Choice2" : "Second choice on the menu" : "C2",
  173.     "Choice3" : "Third choice on the menu"  : "C3"
  174.   ENDMENU
  175.   TO MenuSelection
  176.  
  177.   EXECPROC ProcNames[MenuSelection]
  178.  
  179.   The levels of substitution must be peeled away one layer at a
  180.   time to obtain a direct understanding of the execution process.
  181.  
  182.   First, the variable MenuSelection is replaced with the literal
  183.   string value of the user selected menu choice ("C1" for example):
  184.  
  185.   EXECPROC ProcNames["C1"]
  186.  
  187.   The next substitution requires that the DYNARRAY tag is replaced
  188.   by its stored string value:
  189.  
  190.   EXECPROC "Option1"
  191.  
  192.   Which finally is executed as:
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Paradox                               NUMBER  :  1123
  207.   VERSION  :  4.0
  208.        OS  :  DOS
  209.      DATE  :  October 19, 1992                         PAGE  :  4/4
  210.  
  211.     TITLE  :  An Introduction to DYNARRAY Branching
  212.  
  213.  
  214.  
  215.  
  216.   Option1()
  217.  
  218.   While this may seem like a lot of code just to call a procedure,
  219.   all of the setup is up front in the DYNARRAY declaration and
  220.   assignment of values, and it is only done once.  SWITCH must
  221.   evaluate which Boolean condition is true then call the
  222.   appropriate procedure.  DYNARRAY Branching has the advantage of
  223.   "punching through" the execution layers to directly call a
  224.   procedure.  This is why the performance of SWITCH slows down
  225.   after a certain number of cases while EXECPROC remains constant
  226.   no matter how many elements are stored in the Dynamic Array.
  227.  
  228.   This technique also has serious coding advantages when used in
  229.   the context of the new Event-Driven WAIT command and the
  230.   SHOWDIALOG command.  These techniques are beyond the scope of
  231.   this Tech Info article.  Future articles will elaborate on these
  232.   issues.
  233.  
  234.   DISCLAIMER: You have the right to use this technical information
  235.   subject to the terms of the No-Nonsense License Statement that
  236.   you received with the Borland product to which this information
  237.   pertains.
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.